package com.platform.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.server.common.Constant;
import com.platform.server.dao.AccountDao;
import com.platform.server.dao.ServerDao;
import com.platform.server.dao.WalletDao;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.AccountService;
import com.szrca.ima.utils.StringTools;

@Transactional
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private WalletDao walletDao;

    @Autowired
    private ServerDao serverDao;

    /**
     * 绑定银行卡卡号
     */
    @Override
    public ReturnData bindingBank(String username, String bankName, String bankNum, String id) {
        if (StringTools.isEmpty(username) || StringTools.isEmpty(bankName) || StringTools.isEmpty(bankNum) || StringTools.isEmpty(id)) {
            return new ReturnData(12, "参数有误");
        }
		/*if(!BankUtil.checkBankCard(bankNum)){
			return new ReturnData(13,"银行卡号输入不正确");
		}*/
		/*int num = accountDao.selectBankNum(bankNum);
		if(num > 0){
			return new ReturnData(19,"此银行卡已经存在");
		}*/
        int flag = accountDao.bindingBank(username, bankName, bankNum, id);
        if (flag == 1) {
            return new ReturnData(200, "绑定成功");
        }
        return new ReturnData(100, "绑定失败");
    }

    /**
     * 绑定支付宝账号
     */
    @Override
    public ReturnData bandingZfb(String username, String zfbNum, String id) {
        if (StringTools.isEmpty(username) || StringTools.isEmpty(zfbNum) || StringTools.isEmpty(id)) {
            return new ReturnData(12, "参数有误");
        }
		/*//查看是否已经有此支付宝账号
		int num = accountDao.selectZfbNum(zfbNum);
		if(num > 0){
			return new ReturnData(19,"此账号已经存在");
		}*/
        int flag = accountDao.bandingZfb(username, zfbNum, id);
        if (flag == 1) {
            return new ReturnData(200, "绑定成功");
        }
        return new ReturnData(100, "绑定失败");
    }

    /**
     * 查看绑定支付宝账号
     */
    @Override
    public ReturnData selectBandingZfb(String userid) {
        if (StringTools.isEmpty(userid)) {
            return new ReturnData(12, "参数有误");
        }
        Map<String, Object> map = accountDao.selectBandingZfb(userid);
        if (map != null) {
            return new ReturnData(map, 200, "成功");
        }
        return new ReturnData(100, "未找到相关信息");
    }

    /**
     * 修改绑定支付宝账号
     */
    @Override
    public ReturnData updateBandingZfb(String userid, String username, String zfb_account) {
        if (StringTools.isEmpty(userid) || StringTools.isEmpty(username)) {
            return new ReturnData(12, "参数有误");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user_id", userid);
        map.put("username", username);
        map.put("zfb_account", zfb_account);
		/*if(!StringTools.isEmpty(zfb_account)){
			//查看是否已经有此支付宝账号
			int num = accountDao.selectZfbNum(zfb_account);
			if(num > 0){
				return new ReturnData(19,"此账号已经存在");
			}
		}*/
        int flag = accountDao.updateBandingZfb(map);
        if (flag == 1) {
            return new ReturnData(200, "成功");
        }
        return new ReturnData(100, "失败");
    }

    /**
     * 查看银行卡号
     */
    @Override
    public ReturnData selectBindingBank(String userid) {
        if (StringTools.isEmpty(userid)) {
            return new ReturnData(12, "参数有误");
        }
        Map<String, Object> map = accountDao.selectBindingBank(userid);
        if (map != null) {
            return new ReturnData(map, 200, "成功");
        }
        return new ReturnData(100, "未找到相关信息");
    }

    /**
     * 修改绑定银行卡号
     */
    @Override
    public ReturnData updateBindingBank(String username, String bankName, String bankNum, String userid) {
        if (StringTools.isEmpty(username) || StringTools.isEmpty(bankName) || StringTools.isEmpty(userid)) {
            return new ReturnData(12, "参数有误");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user_id", userid);
        map.put("username", username);
        map.put("bank_name", bankName);
        map.put("bank", bankNum);
		/*if(!StringTools.isEmpty(bankNum)){
			int num = accountDao.selectBankNum(bankNum);
			if(num > 0){
				return new ReturnData(19,"此银行卡已经存在");
			}
		}*/
        int flag = accountDao.updateBindingBank(map);
        if (flag > 0) {
            return new ReturnData(200, "成功");
        }
        return new ReturnData(100, "失败");
    }

    @Override
    public ReturnData deductMoney(String userid) {
    	 /*   if (!StringTools.isEmpty(userid) ) {
            Map<String, Object> map1 = walletDao.selectMywallet(userid);
            if (map1!=null) {
                List<Map<String, Object>> maps = accountDao.userViolateList(userid);
                if (maps.size() > 0) {
                    for (Map<String, Object> map : maps) {
                        // 支付违约金的用户
                        Map<String, Object> mywallet = walletDao.selectMywallet(userid);
                        if(mywallet!=null){
                        	 String money = mywallet.get("money")
                                     .toString();
              String touser_id = map.get("user_id")
                                    .toString();
              String tomoney = map.get("money")
                                  .toString();
              String toorder_id = map.get("order_id")
                                     .toString();
              String id = map.get("id")
                             .toString();
              if (Double.valueOf(money) >= Double.valueOf(tomoney)) {
                  //  获取违约金的用户
                  Map<String, Object> userwakket = walletDao.selectMywallet(touser_id);
                  String user_moeny = map.get("money")
                                         .toString();
                  Map<String, Object> map2 = new HashMap<>();
                  map2.put("userid", touser_id);
                  map2.put("money", Double.parseDouble(user_moeny) + Double.parseDouble(tomoney));
                  serverDao.UpdateUserMoney(map2);
                  Map<String, Object> map4 = serverDao.selectUserMoney("1");
                  String platform_money =  map4.get("money").toString();
                  if(Double.valueOf(money)>=30){
               	  serverDao.insertCancelOrder(touser_id, tomoney, "服务方违约金收入", toorder_id,null,Constant.INCOME,platform_money,null);
                	  Map<String, Object> map3 = new HashMap<>();
                	  map3.put("userid", userid);
                	  map3.put("money", Double.parseDouble(money) - Double.parseDouble(tomoney));
                	  serverDao.insertCancelOrder(userid, tomoney, "服务违约支出", toorder_id,null,Constant.EXPEND,platform_money,null);
                	  accountDao.delete(id);
                  }
                  return new ReturnData(200, "请求成功");
              }	
                        }
                    }
                } else {
                    return new ReturnData(200, "暂未违约记录");
                }
            }
        } else {
            return new ReturnData(200, "参数错误");
        }*/
        return new ReturnData(200, "请充值钱包余额");
    }


}
